<?php
/*
	我的申请
	订单状态说明
		0	待支付
		1	待发货
		2	待收货
		3	已收货
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
cfc('core')->checklogin();
if($_RQ['op']=='list'){
	$list=pdo_getall('jy_move_agentcar_order',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if($list){
		foreach($list as &$item){
			$item['pcd']=json_decode($item['pcd'],true);
		}
	}
	$return['list']=$list;
	$return['statuss']=$this->order_status();
	exi($return);
}
if($_RQ['op']=='add'){
	$item=array();
	$return['item']=$item;
	$return['htmls']=array(
		'title'=>'申请挪车卡',
		'list'=>array(
			array('type'=>'string','name'=>'name','title'=>'姓名'),
			array('type'=>'string','name'=>'mobile','title'=>'电话'),
			array('type'=>'area','name'=>'area_code','title'=>'地区'),
			array('type'=>'string','name'=>'address','title'=>'地址'),
			array('type'=>'pay_submit','submit_url'=>'post','complete_url'=>'list','title'=>'提交'),
		),
	);
	
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['name']){
		exi('请先输入姓名！','error');
	}
	if(!$post['mobile']){
		exi('请先输入电话！','error');
	}
	if(!$post['area_code']){
		exi('请先设置地区！','error');
	}
	if(!$post['address']){
		exi('请先输入详细地址！','error');
	}
	$setting=$this->plugin_setting('card');
	
	$order=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'price'=>intval($setting['price']*100),
		'status'=>0,
		'ordersn'=>get_ordersn('jy_move_agentcar_order','ordersn'),
		'agent_id'=>$this->getcolumn('agent',array('area_code'=>$post['area_code']),'mid'),
		'name'=>$post['name'],
		'mobile'=>$post['mobile'],
		'area_code'=>$post['area_code'],
		'address'=>$post['address'],
		'createtime'=>TIMESTAMP,
	);
	pdo_insert('jy_move_agentcar_order',$order);
	$order_id=pdo_insertid();
	$params=array(
		'id'=>$order_id,
		'title'=>'购买挪车卡',
		'no_type'=>'hdfk',
		'fee'=>$order['price']/100,
		'back_url'=>'jy_move_agentcar/member.apply/pay_result',
	);
	$pay_id=cfc('pay')->create_pay_order($params);
	$this->update('order',array('pay_id'=>$pay_id),array('id'=>$order_id));
	exi($pay_id);
}
if($_RQ['op']=='pay_result'){
	$pay_result=$_RQ['pay_result'];
	$order=$this->get('order',array('id'=>$pay_result['id']));
	if(!$order || $order['status']){
		exi('指定订单不存在或已支付！','error');
	}
	if($order['price']/100!=$pay_result['fee']){
		exi('订单金额不匹配！','error');
	}
	$this->update('order',array('status'=>1),array('id'=>$order['id']));
	//生成佣金信息
	$commission_setting=$this->plugin_setting('commission');
	if($commission_setting['open_commission']){
		$agent_id=$this->getcolumn('member',array('mid'=>$_SESSION['uid']),'agent_id');
		if($agent_id){
			$this->create_commission($agent_id,$order);
		}
	}
	
	//如需发送客服消息或者模版消息，此处编写相关代码
	exi('success');
}
if($_RQ['op']=='cancel'){
	//取消订单，处于待发货状态的订单可以取消订单
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单！','error');
	}
	$order=pdo_get('jy_move_agentcar_order',array('id'=>$id,'mid'=>$_SESSION['uid']),array('id','status'));
	if($order['status']>1){
		exi('订单不支持取消！','error');
	}
	pdo_update('jy_move_agentcar_order',array('status'=>4),array('id'=>$id));
	exi('取消成功！','','list');
}
if($_RQ['op']=='get'){
	//确认收货
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单！','error');
	}
	$order=pdo_get('jy_move_agentcar_order',array('id'=>$id,'mid'=>$_SESSION['uid']),array('id','status'));
	if($order['status']!=2){
		exi('订单状态错误！','error');
	}
	pdo_update('jy_move_agentcar_order',array('status'=>3),array('id'=>$id));
	exi('收货成功！','','list');
}